package com.hsmw.api.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hsmw.api.config.jwt.CurrentHsmwUserUtils;
import com.hsmw.api.config.jwt.LoginUser;
import com.hsmw.api.mapper.HsmwDictTypeMapper;
import com.hsmw.api.mapper.HsmwDispatchOrderRemarkMapper;
import com.hsmw.api.mapper.HsmwHtnUserMapper;
import com.hsmw.api.vo.DispatchOrderRemarkVO;
import com.hsmw.common.base.BaseService;
import com.htn.common.core.base.DataResponse;
import com.htn.common.core.constant.CommonConstants;
import com.hsmw.domain.HsmwDictType;
import com.hsmw.domain.HsmwDispatchOrderRemark;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

@Service
@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class HsmwDispatchOrderRemarkService extends BaseService<HsmwDispatchOrderRemarkMapper, HsmwDispatchOrderRemark> {


    @Autowired
    private HsmwDispatchOrderRemarkMapper hsmwDispatchOrderRemarkMapper;

    @Autowired
    private CurrentHsmwUserUtils currentHsmwUserUtils;

    public DataResponse addOrUpdateDispatchRemark(DispatchOrderRemarkVO vo) {
        LoginUser loginUser = currentHsmwUserUtils.getCurrentUserInfo();

        //修改或者新增调度的备注之前首先把 之前的调度备注删掉
        hsmwDispatchOrderRemarkMapper.delete(new LambdaQueryWrapper<HsmwDispatchOrderRemark>()
                .eq(HsmwDispatchOrderRemark::getRequestId,vo.getRequestId()));
        //然后再 新增
        if(CollectionUtil.isNotEmpty(vo.getDispatchOrderRemarks())){
            List<HsmwDispatchOrderRemark> remarkList = vo.getDispatchOrderRemarks();
            remarkList.stream().forEach(hsmwDispatchOrderRemark -> {
                hsmwDispatchOrderRemark.setRequestId(vo.getRequestId());
                hsmwDispatchOrderRemark.setUserId(loginUser.getId());
                hsmwDispatchOrderRemark.setUsername(loginUser.getUsername());
            });
            saveBatch(remarkList);
        }
        return new DataResponse(CommonConstants.ResponseStatus.SUCCESS,"保存成功");    }
}
